﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using yiCommerce.Core;
using yiCommerce.Models.Orders;
using yiCommerce.Service.Orders;

namespace yiCommerce.Controllers.api
{
    public class OrderApiController : ApiController
    {



        private IWorkContext _workContext;

        private IOrderService _orderService;
        private IOrderProcessingService _orderProcessingService;

        public OrderApiController(IWorkContext workContext, IOrderService orderService, IOrderProcessingService orderProcessingService)
        {
            _workContext = workContext;
            _orderService = orderService;
            _orderProcessingService = orderProcessingService;
        }

        public ConfirmOrderResult ConfirmOrder(ConfirmOrderModel model)
        {

            var result = new ConfirmOrderResult();
            var orderId = model.OrderId;
            var order = _orderService.GetOrderByIdAndCustomerId(orderId, _workContext.CurrentCustomer.Id);
            if (order == null)
            {
                result.Status = "1";
                result.Message = string.Format("order {0} does not exist", orderId);
                return result;
            }

            else
            {
                try
                {

                    _orderProcessingService.MarkOrderAsCompleted(order.Id);
                    _orderProcessingService.MarkOrderDelivered(order.Id);


                    result.Status = "0";
                }
                catch (Exception e)
                {
                    result.Status = "1";
                    result.Message = e.Message;
                }
            }

            return result;

        }
    }

    public class ConfirmOrderResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int Id { get; set; }
    }
}
